package other.edan.newpaas.bid;

import com.rkhd.platform.sdk.creekflow.ruleevent.RuleEvent;
import com.rkhd.platform.sdk.creekflow.ruleevent.RuleEventRequest;
import com.rkhd.platform.sdk.creekflow.ruleevent.RuleEventResponse;
import com.rkhd.platform.sdk.data.model.BidManagement__c;
import com.rkhd.platform.sdk.data.model.Lead;
import com.rkhd.platform.sdk.data.model.Opportunity;
import com.rkhd.platform.sdk.exception.ScriptBusinessException;
import com.rkhd.platform.sdk.log.Logger;
import com.rkhd.platform.sdk.log.LoggerFactory;
import org.apache.commons.lang.StringUtils;
import other.edan.newpaas.utils.HCXObjectService;
import other.edan.newpaas.utils.Result;

/**
 * @Description: 根据机会获线索的信息（招投标信息）
 * @Author: 黎炎松
 * @Date: 2024/12/12 16:28
 */
public class SetLeadInfo implements RuleEvent {
    private static final Logger LOGGER = LoggerFactory.getLogger();
    private static String LOGGER_MSG = "根据机会获招投标的信息 ==> ";

    /**
     * 执行触发规则触发事件
     * @param request
     * @return
     * @throws ScriptBusinessException
     */
    @Override
    public RuleEventResponse execute(RuleEventRequest request) throws ScriptBusinessException {
        LOGGER.info(LOGGER_MSG + "执行触发事件");
        getLeadInfo(request.getDataId());

        RuleEventResponse response = new RuleEventResponse();
        response.setSuccess(true);
        response.setMsg("success");
        return response;
    }


    public static void main(String[] args) {
        new SetLeadInfo().getLeadInfo(3591825975548522L);
    }


    public Result getLeadInfo(Long bidId) {
        LOGGER.info(LOGGER_MSG + "处理开始");
        BidManagement__c bidManagement = new BidManagement__c();
        bidManagement.setId(bidId);
        bidManagement = HCXObjectService.get(bidManagement);

        // 获取业务机会
        Long optId = bidManagement.getOpportunity__c();
        if (optId == null) {
            LOGGER.info(LOGGER_MSG + "关联项目为空，无需处理");
            return Result.success();
        }

        // 获取 招投标项目名称
        Opportunity opportunity = new Opportunity();
        opportunity.setId(optId);
        opportunity = HCXObjectService.get(opportunity);
        String bidProjectName = opportunity.getBidProjectName__c(); // 招投标项目名称
        if (StringUtils.isBlank(bidProjectName)) {
            LOGGER.info(LOGGER_MSG + "招投标项目名称为空，无需处理");
            return Result.success();
        }

        // 赋值
        String sql = String.format("select id,bidOpeningTime__c,itemNum__c,customItem195__c,customItem206__c from lead where customItem192__c = '%s'", bidProjectName);
        Lead lead = HCXObjectService.queryOne(sql);
        if (lead == null) {
            LOGGER.info(LOGGER_MSG + "查询不到对应的招投标信息");
            return Result.success();
        }

        BidManagement__c upBid = new BidManagement__c();
        upBid.setId(bidId);
        upBid.setOpenDate__c(lead.getBidOpeningTime__c());
        upBid.setProjectNumber__c(lead.getItemNum__c());
        upBid.setBidId__c(lead.getCustomItem195__c());
        upBid.setWinBidId__c(lead.getCustomItem206__c());
        HCXObjectService.update(upBid);
        LOGGER.info(LOGGER_MSG + "处理成功");

        return Result.success();
    }


}
